<?php
namespace Admin\Controller;

class SadminController extends BaseController {


    public function index() {

        $Sadmin = D ('Sadmin');

        $map = null;

        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Sadmin->getCountByWhere($map),10);

        $sadminList = $Sadmin->getSadminByWhere($map,$limit);

        $this->assign('sadminList',$sadminList);
        $this->assign('page',$page);
        $this->display();
    }


    public function add() {

        if (!IS_POST) {
            $this->display();
        } else {

            $data['account'] = $_REQUEST['account'];
            $data['email'] = $_REQUEST['email'];
            $data['telphone'] = $_REQUEST['telphone'];
            $data['tname'] = $_REQUEST['tname'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $data['password'] = md5('123456');

            $Sadmin = D ('Sadmin');
            $flag = $Sadmin->addSadmin($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }

    }


    public function edit() {

        if (!IS_POST) {

            $id = $_REQUEST['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Sadmin = D ('Sadmin');
            $s = $Sadmin->getSadminById($id);
            if (empty($s)) $this->error('管理员不存在');

            $this->assign('sadmin',$s);
            $this->display();

        } else {

            $data['id'] = $_REQUEST['id'];
            $data['email'] = $_REQUEST['email'];
            $data['telphone'] = $_REQUEST['telphone'];
            $data['tname'] = $_REQUEST['tname'];
            $data['password'] = md5($_REQUEST['password']);

            if ($data['password'] === md5('default')) unset($data['password']);

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Sadmin = D ('Sadmin');
            $flag = $Sadmin->editSadmin($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }

    }



    public function del() {
        if (!IS_POST) $this->error('非法访问');

        $id = $_REQUEST['id'];
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Sadmin = D ('Sadmin');
        $flag = $Sadmin->delSadmin($id);
        if ($flag === '000000')
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

    }

}